using UnityEngine;

[AddComponentMenu("Scripts/Area Damage/DestroyBodyJoints")]
public class DestroyBodyJoints : EventComponentArea<DestroyBodyJoints>
{
	public Rigidbody2D body;

	[Tooltip("Импульс для разброса тел из точки соединения в направлении центра массы тела")]
	public int impulse;

	[Tooltip("Добавить вращение тела")]
	public float torque;

	private HingeJoint2D hingleJoint;

	private Joint2D[] listDestroy;

	public override bool ShowIconGizmos(AreaDamage area)
	{
		return body != null;
	}

	public override void Activation(AreaDamage area)
	{
		if (body.transform.parent != null)
		{
			listDestroy = body.transform.parent.GetComponentsInChildren<Joint2D>(includeInactive: true);
		}
		else
		{
			listDestroy = Object.FindObjectsOfType<Joint2D>();
		}
		for (int i = 0; i < listDestroy.Length; i++)
		{
			if (listDestroy[i].gameObject == body.gameObject || listDestroy[i].connectedBody == body)
			{
				if (hingleJoint == null && listDestroy[i] is HingeJoint2D)
				{
					hingleJoint = (listDestroy[i] as HingeJoint2D);
				}
				else
				{
					UnityEngine.Object.Destroy(listDestroy[i]);
				}
			}
		}
		listDestroy = null;
		if ((impulse != 0 || torque != 0f) && hingleJoint != null)
		{
			Vector2 vector = default(Vector2);
			Vector2 vector2 = default(Vector2);
			body = hingleJoint.GetComponent<Rigidbody2D>();
			vector = hingleJoint.transform.TransformPoint(hingleJoint.anchor);
			Vector2 worldCenterOfMass = body.worldCenterOfMass;
			vector2.x = worldCenterOfMass.x - vector.x;
			Vector2 worldCenterOfMass2 = body.worldCenterOfMass;
			vector2.y = worldCenterOfMass2.y - vector.y;
			vector2.Normalize();
			vector2 *= impulse;
			body.isKinematic = false;
			body.AddForceAtPosition(vector2, vector);
			body.AddTorque(torque);
			Vector2 worldCenterOfMass3 = hingleJoint.connectedBody.worldCenterOfMass;
			vector2.x = worldCenterOfMass3.x - vector.x;
			Vector2 worldCenterOfMass4 = hingleJoint.connectedBody.worldCenterOfMass;
			vector2.y = worldCenterOfMass4.y - vector.y;
			vector2.Normalize();
			vector2 *= impulse;
			if (hingleJoint.connectedBody.simulated && hingleJoint.connectedBody.gameObject.activeInHierarchy)
			{
				hingleJoint.connectedBody.isKinematic = false;
				hingleJoint.connectedBody.AddForceAtPosition(vector2, vector);
				hingleJoint.connectedBody.AddTorque(0f - torque);
			}
		}
		if (hingleJoint != null)
		{
			UnityEngine.Object.Destroy(hingleJoint);
			hingleJoint = null;
		}
		if (onActivationListener != null)
		{
			onActivationListener(this);
		}
		UnityEngine.Object.Destroy(this);
	}
}
